home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / MPW / gzip 1.2.2 / unlzw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-11  |  8.7 KB  |  388 lines  |  [TEXT/MPS ]

  1. /* unlzw.c -- decompress files in LZW format.
  2.  * The code in this file is directly derived from the public domain 'compress'
  3.  * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
  4.  * Ken Turkowski, Dave Mack and Peter Jannesen.
  5.  *
  6.  * This is a temporary version which will be rewritten in some future version
  7.  * to accommodate in-memory decompression.
  8.  */
  9.  
  10. #ifndef lint
  11. static char rcsid[] = "$Id: unlzw.c,v 0.15 1993/06/10 13:28:35 jloup Exp $";
  12. #endif
  13.  
  14. #ifdef macintosh
  15. #include <cursorctl.h>
  16. #include <types.h>
  17. #else
  18. #include <sys/types.h>
  19. #endif
  20.  
  21. #include "tailor.h"
  22.  
  23. #ifdef HAVE_UNISTD_H
  24. #  include <unistd.h>
  25. #endif
  26. #ifndef NO_FCNTL_H
  27. #  include <fcntl.h>
  28. #endif
  29.  
  30. #include "gzip.h"
  31. #include "lzw.h"
  32.  
  33. typedef    unsigned char    char_type;
  34. typedef          long   code_int;
  35. typedef unsigned long     count_int;
  36. typedef unsigned short    count_short;
  37. typedef unsigned long     cmp_code_int;
  38.  
  39. #define MAXCODE(n)    (1L << (n))
  40.     
  41. #ifndef    REGISTERS
  42. #    define    REGISTERS    2
  43. #endif
  44. #define    REG1    
  45. #define    REG2    
  46. #define    REG3    
  47. #define    REG4    
  48. #define    REG5    
  49. #define    REG6    
  50. #define    REG7    
  51. #define    REG8    
  52. #define    REG9    
  53. #define    REG10
  54. #define    REG11    
  55. #define    REG12    
  56. #define    REG13
  57. #define    REG14
  58. #define    REG15
  59. #define    REG16
  60. #if REGISTERS >= 1
  61. #    undef    REG1
  62. #    define    REG1    register
  63. #endif
  64. #if REGISTERS >= 2
  65. #    undef    REG2
  66. #    define    REG2    register
  67. #endif
  68. #if REGISTERS >= 3
  69. #    undef    REG3
  70. #    define    REG3    register
  71. #endif
  72. #if REGISTERS >= 4
  73. #    undef    REG4
  74. #    define    REG4    register
  75. #endif
  76. #if REGISTERS >= 5
  77. #    undef    REG5
  78. #    define    REG5    register
  79. #endif
  80. #if REGISTERS >= 6
  81. #    undef    REG6
  82. #    define    REG6    register
  83. #endif
  84. #if REGISTERS >= 7
  85. #    undef    REG7
  86. #    define    REG7    register
  87. #endif
  88. #if REGISTERS >= 8
  89. #    undef    REG8
  90. #    define    REG8    register
  91. #endif
  92. #if REGISTERS >= 9
  93. #    undef    REG9
  94. #    define    REG9    register
  95. #endif
  96. #if REGISTERS >= 10
  97. #    undef    REG10
  98. #    define    REG10    register
  99. #endif
  100. #if REGISTERS >= 11
  101. #    undef    REG11
  102. #    define    REG11    register
  103. #endif
  104. #if REGISTERS >= 12
  105. #    undef    REG12
  106. #    define    REG12    register
  107. #endif
  108. #if REGISTERS >= 13
  109. #    undef    REG13
  110. #    define    REG13    register
  111. #endif
  112. #if REGISTERS >= 14
  113. #    undef    REG14
  114. #    define    REG14    register
  115. #endif
  116. #if REGISTERS >= 15
  117. #    undef    REG15
  118. #    define    REG15    register
  119. #endif
  120. #if REGISTERS >= 16
  121. #    undef    REG16
  122. #    define    REG16    register
  123. #endif
  124.     
  125. #ifndef    BYTEORDER
  126. #    define    BYTEORDER    0000
  127. #endif
  128.     
  129. #ifndef    NOALLIGN
  130. #    define    NOALLIGN    0
  131. #endif
  132.  
  133.  
  134. union    bytes {
  135.     long  word;
  136.     struct {
  137. #if BYTEORDER == 4321
  138.     char_type    b1;
  139.     char_type    b2;
  140.     char_type    b3;
  141.     char_type    b4;
  142. #else
  143. #if BYTEORDER == 1234
  144.     char_type    b4;
  145.     char_type    b3;
  146.     char_type    b2;
  147.     char_type    b1;
  148. #else
  149. #    undef    BYTEORDER
  150.     int  dummy;
  151. #endif
  152. #endif
  153.     } bytes;
  154. };
  155.  
  156. #if BYTEORDER == 4321 && NOALLIGN == 1
  157. #  define input(b,o,c,n,m){ \
  158.      (c) = (*(long *)(&(b)[(o)>>3])>>((o)&0x7))&(m); \
  159.      (o) += (n); \
  160.    }
  161. #else
  162. #  define input(b,o,c,n,m){ \
  163.      REG1 char_type *p = &(b)[(o)>>3]; \
  164.      (c) = ((((long)(p[0]))|((long)(p[1])<<8)| \
  165.      ((long)(p[2])<<16))>>((o)&0x7))&(m); \
  166.      (o) += (n); \
  167.    }
  168. #endif
  169.  
  170. #ifndef MAXSEG_64K
  171.    /* DECLARE(ush, tab_prefix, (1<<BITS)); -- prefix code */
  172. #  define tab_prefixof(i) tab_prefix[i]
  173. #  define clear_tab_prefixof()    memzero(tab_prefix, 256);
  174. #else
  175.    /* DECLARE(ush, tab_prefix0, (1<<(BITS-1)); -- prefix for even codes */
  176.    /* DECLARE(ush, tab_prefix1, (1<<(BITS-1)); -- prefix for odd  codes */
  177.    ush *tab_prefix[2];
  178. #  define tab_prefixof(i) tab_prefix[(i)&1][(i)>>1]
  179. #  define clear_tab_prefixof()    \
  180.       memzero(tab_prefix0, 128), \
  181.       memzero(tab_prefix1, 128);
  182. #endif
  183. #ifdef macintosh
  184. #define de_stack    (char_type *)(d_buf + DIST_BUFSIZE - 1)
  185. #else
  186. #define de_stack        ((char_type *)(&d_buf[DIST_BUFSIZE-1]))
  187. #endif
  188. #define tab_suffixof(i) tab_suffix[i]
  189.  
  190. int block_mode = BLOCK_MODE; /* block compress mode -C compatible with 2.0 */
  191.  
  192. /* ============================================================================
  193.  * Decompress in to out.  This routine adapts to the codes in the
  194.  * file building the "string" table on-the-fly; requiring no table to
  195.  * be stored in the compressed file.
  196.  * IN assertions: the buffer inbuf contains already the beginning of
  197.  *   the compressed data, from offsets iptr to insize-1 included.
  198.  *   The magic header has already been checked and skipped.
  199.  *   bytes_in and bytes_out have been initialized.
  200.  */
  201. int unlzw(in, out) 
  202.     int in, out;    /* input and output file descriptors */
  203. {
  204.     REG2   char_type  *stackp;
  205.     REG3   code_int   code;
  206.     REG4   int        finchar;
  207.     REG5   code_int   oldcode;
  208.     REG6   code_int   incode;
  209.     REG7   long       inbits;
  210.     REG8   long       posbits;
  211.     REG9   int        outpos;
  212. /*  REG10  int        insize; (global) */
  213.     REG11  unsigned   bitmask;
  214.     REG12  code_int   free_ent;
  215.     REG13  code_int   maxcode;
  216.     REG14  code_int   maxmaxcode;
  217.     REG15  int        n_bits;
  218.     REG16  int        rsize;
  219.     
  220. #ifdef MAXSEG_64K
  221.     tab_prefix[0] = tab_prefix0;
  222.     tab_prefix[1] = tab_prefix1;
  223. #endif
  224.     maxbits = get_byte();
  225.     block_mode = maxbits & BLOCK_MODE;
  226.     if ((maxbits & LZW_RESERVED) != 0) {
  227.     WARN((stderr, "\n%s: %s: warning, unknown flags 0x%x\n",
  228.           progname, ifname, maxbits & LZW_RESERVED));
  229.     }
  230.     maxbits &= BIT_MASK;
  231.     maxmaxcode = MAXCODE(maxbits);
  232.     
  233.     if (maxbits > BITS) {
  234.     fprintf(stderr,
  235.         "\n%s: %s: compressed with %d bits, can only handle %d bits\n",
  236.         progname, ifname, maxbits, BITS);
  237.     exit_code = ERROR;
  238.     return ERROR;
  239.     }
  240.     rsize = insize;
  241.     maxcode = MAXCODE(n_bits = INIT_BITS)-1;
  242.     bitmask = (1<<n_bits)-1;
  243.     oldcode = -1;
  244.     finchar = 0;
  245.     outpos = 0;
  246.     posbits = inptr<<3;
  247.  
  248.     free_ent = ((block_mode) ? FIRST : 256);
  249.     
  250.     clear_tab_prefixof(); /* Initialize the first 256 entries in the table. */
  251.     
  252.     for (code = 255 ; code >= 0 ; --code) {
  253.     tab_suffixof(code) = (char_type)code;
  254.     }
  255.     do {
  256.     REG1 int i;
  257.     int  e;
  258.     int  o;
  259.  
  260. #ifdef macintosh
  261.     SpinCursor( -1 );
  262. #endif
  263.     
  264.     resetbuf:
  265.     e = insize-(o = (posbits>>3));
  266.     
  267.     for (i = 0 ; i < e ; ++i) {
  268.         inbuf[i] = inbuf[i+o];
  269.     }
  270.     insize = e;
  271.     posbits = 0;
  272.     
  273.     if (insize < INBUF_EXTRA) {
  274.         if ((rsize = read(in, (char*)inbuf+insize, INBUFSIZ)) == EOF) {
  275.         read_error();
  276.         }
  277.         insize += rsize;
  278.         bytes_in += (ulg)rsize;
  279.     }
  280.     inbits = ((rsize != 0) ? ((long)insize - insize%n_bits)<<3 : 
  281.           ((long)insize<<3)-(n_bits-1));
  282.     
  283.     while (inbits > posbits) {
  284.         if (free_ent > maxcode) {
  285.         posbits = ((posbits-1) +
  286.                ((n_bits<<3)-(posbits-1+(n_bits<<3))%(n_bits<<3)));
  287.         ++n_bits;
  288.         if (n_bits == maxbits) {
  289.             maxcode = maxmaxcode;
  290.         } else {
  291.             maxcode = MAXCODE(n_bits)-1;
  292.         }
  293.         bitmask = (1<<n_bits)-1;
  294.         goto resetbuf;
  295.         }
  296.         input(inbuf,posbits,code,n_bits,bitmask);
  297.         
  298.         if (oldcode == -1) {
  299.         outbuf[outpos++] = (char_type)(finchar = (int)(oldcode=code));
  300.         continue;
  301.         }
  302.         if (code == CLEAR && block_mode) {
  303.         clear_tab_prefixof();
  304.         free_ent = FIRST - 1;
  305.         posbits = ((posbits-1) +
  306.                ((n_bits<<3)-(posbits-1+(n_bits<<3))%(n_bits<<3)));
  307.         maxcode = MAXCODE(n_bits = INIT_BITS)-1;
  308.         bitmask = (1<<n_bits)-1;
  309.         goto resetbuf;
  310.         }
  311.         incode = code;
  312.         stackp = de_stack;
  313.         
  314.         if (code >= free_ent) { /* Special case for KwKwK string. */
  315.         if (code > free_ent) {
  316. #ifdef DEBUG            
  317.             char_type *p;
  318.  
  319.             posbits -= n_bits;
  320.             p = &inbuf[posbits>>3];
  321.             fprintf(stderr,
  322.                 "code:%ld free_ent:%ld n_bits:%d insize:%u\n",
  323.                 code, free_ent, n_bits, insize);
  324.             fprintf(stderr,
  325.                 "posbits:%ld inbuf:%02X %02X %02X %02X %02X\n",
  326.                 posbits, p[-1],p[0],p[1],p[2],p[3]);
  327. #endif
  328.             if (!test && outpos > 0) {
  329.             write_buf(out, (char*)outbuf, outpos);
  330.             bytes_out += (ulg)outpos;
  331.             }
  332.             error("corrupt input. Use zcat to recover some data.");
  333.         }
  334.         *--stackp = (char_type)finchar;
  335.         code = oldcode;
  336.         }
  337.  
  338.         while ((cmp_code_int)code >= (cmp_code_int)256) {
  339.         /* Generate output characters in reverse order */
  340.         *--stackp = tab_suffixof(code);
  341.         code = tab_prefixof(code);
  342.         }
  343.         *--stackp =    (char_type)(finchar = tab_suffixof(code));
  344.         
  345.         /* And put them out in forward order */
  346.         {
  347.         REG1 int    i;
  348.         
  349.         if (outpos+(i = (de_stack-stackp)) >= OUTBUFSIZ) {
  350.             do {
  351.             if (i > OUTBUFSIZ-outpos) i = OUTBUFSIZ-outpos;
  352.  
  353.             if (i > 0) {
  354.                 memcpy(outbuf+outpos, stackp, i);
  355.                 outpos += i;
  356.             }
  357.             if (outpos >= OUTBUFSIZ) {
  358.                 if (!test) {
  359.                 write_buf(out, (char*)outbuf, outpos);
  360.                 bytes_out += (ulg)outpos;
  361.                 }
  362.                 outpos = 0;
  363.             }
  364.             stackp+= i;
  365.             } while ((i = (de_stack-stackp)) > 0);
  366.         } else {
  367.             memcpy(outbuf+outpos, stackp, i);
  368.             outpos += i;
  369.         }
  370.         }
  371.  
  372.         if ((code = free_ent) < maxmaxcode) { /* Generate the new entry. */
  373.  
  374.         tab_prefixof(code) = (unsigned short)oldcode;
  375.         tab_suffixof(code) = (char_type)finchar;
  376.         free_ent = code+1;
  377.         } 
  378.         oldcode = incode;    /* Remember previous code.    */
  379.     }
  380.     } while (rsize != 0);
  381.     
  382.     if (!test && outpos > 0) {
  383.     write_buf(out, (char*)outbuf, outpos);
  384.     bytes_out += (ulg)outpos;
  385.     }
  386.     return OK;
  387. }
  388.